package cn.com.chaochuang.gxfdaoa.pdfserver;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import java.text.SimpleDateFormat;

/**
 * 2017-12-26
 *
 * @author Shicx
 */
public class PDFHelper {

    public static final SimpleDateFormat FOLDER_NAME_FMT = new SimpleDateFormat("yyy-MM-dd");

    /**
     * word保存为pdf格式宏，值为17
     */
    private static final int WD_FORMAT_PDF = 17;
    private static final int XL_TYPE_PDF = 0;
    private static final int PPT_SAVE_AS_PDF = 32;

    /**
     * wps 转 pdf
     */
    private final static String WPS_WORD_SERVER="KWPS.Application";
    private final static String WPS_PPT_SERVER="KWPP.Application";
    private final static String WPS_EXCEL_SERVER="KET.Application";

    /**
     * Wps 2015 word转pdf
     * @param srcFilePath
     * @param pdfFilePath
     * @return
     */
    public static boolean wpsDoc2Pdf(String srcFilePath, String pdfFilePath) {
        ActiveXComponent pptActiveXComponent=null;
        ActiveXComponent workbook = null;
        try {
            //初始化COM线程
            ComThread.InitSTA();
            //初始化exe程序
            pptActiveXComponent = new ActiveXComponent(WPS_WORD_SERVER);
            Variant[] openParams=new Variant[]{
                    //filePath
                    new Variant(srcFilePath),
                    new Variant(true),
                    //readOnly
                    new Variant(true)
            };
            workbook = pptActiveXComponent.invokeGetComponent("Documents").invokeGetComponent("Open",openParams);
            workbook.invoke("SaveAs",new Variant(pdfFilePath),new Variant(WD_FORMAT_PDF));
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(workbook!=null)
            {
                workbook.invoke("Close");
                workbook.safeRelease();
            }
            if(pptActiveXComponent!=null)
            {
                pptActiveXComponent.invoke("Quit");
                pptActiveXComponent.safeRelease();
            }
            ComThread.Release();
        }
        return false;
    }

    /**
     * Wps 2015 ppt转pdf
     * @param srcFilePath
     * @param pdfFilePath
     * @return
     */
    public static boolean wpsPpt2Pdf(String srcFilePath, String pdfFilePath) {
        ActiveXComponent pptActiveXComponent=null;
        ActiveXComponent workbook = null;
        try {
            //初始化COM线程
            ComThread.InitSTA();
            //初始化exe程序
            pptActiveXComponent = new ActiveXComponent(WPS_PPT_SERVER);
            workbook = pptActiveXComponent.invokeGetComponent("Presentations").invokeGetComponent("Open",new Variant(srcFilePath),new Variant(true));
            workbook.invoke("SaveAs",new Variant(pdfFilePath),new Variant(PPT_SAVE_AS_PDF));
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(workbook!=null)
            {
                workbook.invoke("Close");
                workbook.safeRelease();
            }
            if(pptActiveXComponent!=null)
            {
                pptActiveXComponent.invoke("Quit");
                pptActiveXComponent.safeRelease();
            }
            ComThread.Release();
        }
        return false;
    }

    /**
     * Wps 2015 excel转pdf
     * @param srcFilePath
     * @param pdfFilePath
     * @return
     */
    public static boolean wpsExcel2Pdf(String srcFilePath,String pdfFilePath){
        ActiveXComponent et = null;
        Dispatch workbooks = null;
        Dispatch workbook = null;
        //初始化COM线程
        ComThread.InitSTA();
        try {
            //初始化et.exe程序
            et = new ActiveXComponent(WPS_EXCEL_SERVER);
            et.setProperty("Visible", new Variant(false));
            workbooks = et.getProperty("Workbooks").toDispatch();
            workbook = Dispatch.invoke(workbooks,"Open",Dispatch.Method,new Object[]{srcFilePath,0,true},new int[1]).toDispatch();
            Dispatch.call(workbook,"ExportAsFixedFormat",new Object[]{XL_TYPE_PDF,pdfFilePath});
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(workbook!=null)
            {
                Dispatch.call(workbook,"Close");
                workbook.safeRelease();
            }
            if(et!=null)
            {
                et.invoke("Quit");
                et.safeRelease();
            }
            ComThread.Release();
        }
        return false;
    }


    /**
     * MS Office2010
     * @param srcFilePath
     * @param pdfFilePath
     * @return
     */
    public static boolean doc2pdf(String srcFilePath, String pdfFilePath) {
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            ComThread.InitSTA();
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", false);
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
                    new Object[] { srcFilePath,
                            new Variant(false),
                            new Variant(true),
                            new Variant(false),
                            new Variant("pwd") },
                    new int[1]).toDispatch();
            Dispatch.put(doc, "RemovePersonalInformation", false);
            // word保存为pdf格式宏，值为17
            Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath,WD_FORMAT_PDF);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (doc != null) {
                Dispatch.call(doc, "Close", false);
            }
            if (app != null) {
                app.invoke("Quit", 0);
            }
            ComThread.Release();
        }
        return false;
    }

    /**
     *  MS Office2010
     * @param srcFilePath
     * @param pdfFilePath
     * @return
     */
    public static boolean ppt2pdf(String srcFilePath, String pdfFilePath) {
        ActiveXComponent app = null;
        Dispatch ppt = null;
        try {
            ComThread.InitSTA();
            app = new ActiveXComponent("PowerPoint.Application");
            Dispatch ppts = app.getProperty("Presentations").toDispatch();

            // 因POWER.EXE的发布规则为同步，所以设置为同步发布
            ppt = Dispatch.call(ppts, "Open", srcFilePath, true,true, false
            ).toDispatch();
            //ppSaveAsPDF为特定值32
            Dispatch.call(ppt, "SaveAs", pdfFilePath, PPT_SAVE_AS_PDF);
            return true; // set flag true;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (ppt != null) {
                Dispatch.call(ppt, "Close");
            }
            if (app != null) {
                app.invoke("Quit");
            }
            ComThread.Release();
        }
        return false;
    }

    /**
     * MS Office2010
     * @param inputFile
     * @param pdfFile
     * @return
     */
    public static boolean excel2Pdf(String inputFile,String pdfFile){
        ActiveXComponent app=null;
        Dispatch excel =null;
        try{
            ComThread.InitSTA();
            app = new ActiveXComponent("Excel.Application");
            app.setProperty("Visible", false);
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            excel = Dispatch.call(excels,"Open",inputFile, false,true).toDispatch();
            Dispatch.call(excel,"ExportAsFixedFormat", XL_TYPE_PDF,pdfFile);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if (excel!= null) {
                Dispatch.call(excel, "Close");
            }
            if (app != null) {
                app.invoke("Quit");
            }
            ComThread.Release();
        }
        return false;
    }
}
